<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/26
 * Time: 17:04
 */

namespace app\ext\controller;


use think\Controller;
use think\Loader;

class Image extends Controller
{
    public function index()
    {
        $this->assign('title','图片处理');
        return view('index');
    }

    /**
     * @param int $asdc
     */
    public function QR()
    {
        Loader::import('Qrcode.phpqrcode');

        $url = url('ext/image/index');
        $errorCorrectionLevel = "L";
        $matrixPointSize = 5;

        $filename =  time() . '.png';
        \QRcode::png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
        die;

        $QR = imagecreatefromstring(file_get_contents($filename));//创建一块画布，并从字符串中的图像流新建一副图像

        imagepng($QR,time() . '.png');
        imagedestroy($QR);
        echo  '<img src="/'. time() . '.png" alt="二维码" style="width:400px;height:400px;" />';
    }

    public function QRLogo()
    {
        Loader::import('Qrcode.phpqrcode');
        $filename = time() . '_logo.png';
        $url = url('/ext/image/index');

        \QRcode::png($url,$filename,'L',5,2);

        $logo = 'x.png';
        $QR = imagecreatefromstring(file_get_contents($filename));

        if(file_exists($logo))
        {
            $logo_source = imagecreatefromstring(file_get_contents(($logo)));

            $QR_width = imagesx($QR);
            $QR_height = imagesy($QR);
            $logo_width = imagesx($logo_source);
            $logo_height = imagesy($logo_source);
            $logo_qr_width = $QR_width / 4;
            $scale = $logo_width / $logo_qr_width;
            $logo_qr_height = $logo_height / $scale;
            $from_width = ($QR_width - $logo_qr_width) /2;

            imagecopyresampled($QR, $logo_source, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
        }
        imagepng($QR);
        $s = ob_get_clean();
        header("Content-Type: image/jpeg");
        echo $s;
        imagedestroy($QR);
/*        imagepng($QR, time() . '_logo.png');
        imagedestroy($QR);
        imagedestroy($logo_source);
        echo "<img src='/" . time() . "_logo.png' style='width:400;height:400px;' />";

        /*
        //下载文件
                 imagepng($QR);
                $s = ob_get_clean();
                header("Content-Type: image/jpeg");
                header('Content-Disposition: attachment; filename="2017博导前程杯电商大赛初赛证书.png"');
                header('Content-Length: '.strlen($s));
                echo $s;
                imagedestroy($QR);*/
    }
}
